<?php
declare(strict_types=1);

namespace SixShop\Message\Controller\Api;

use think\Request;

/**
 * 用户通知API控制器
 */
class NotificationController extends BaseController
{
    /**
     * 获取用户通知列表
     */
    public function getList(Request $request)
    {
        $userId = (int)$this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $page = $request->param('page/d', 1);
        $limit = $request->param('limit/d', 10);
        $type = $request->param('type/d', 0);
        $isRead = $request->param('is_read/d', null);
        
        $params = [];
        if ($type > 0) {
            $params['type'] = $type;
        }
        if ($isRead !== null) {
            $params['is_read'] = $isRead;
        }
        
        $result = $this->notificationModel->getUserNotifications($userId, $params, $page, $limit);
        
        return $this->success('获取成功', $result);
    }
    
    /**
     * 获取通知详情
     */
    public function getDetail(Request $request)
    {
        $userId = (int)$this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $detail = $this->notificationModel->getDetail($id);
        if (!$detail) {
            return $this->error('通知不存在');
        }
        
        // 检查权限
        if ($detail->user_id > 0 && $detail->user_id != $userId) {
            return $this->error('无权限查看');
        }
        
        // 标记为已读
        $this->notificationModel->markAsRead($id, $userId);
        
        return $this->success('获取成功', $detail);
    }
    
    /**
     * 标记通知为已读
     */
    public function markAsRead(Request $request)
    {
        $userId = (int)$this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->notificationModel->markAsRead($id, $userId);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('操作成功');
    }
    
    /**
     * 批量标记通知为已读
     */
    public function batchMarkAsRead(Request $request)
    {
        $userId = (int)$this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $ids = $request->param('ids');
        if (empty($ids)) {
            return $this->error('参数错误');
        }
        
        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }
        
        $result = $this->notificationModel->batchMarkAsRead($ids, $userId);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('操作成功');
    }
    
    /**
     * 标记所有通知为已读
     */
    public function markAllAsRead(Request $request)
    {
        $userId = (int)$this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $result = $this->notificationModel->markAllAsRead($userId);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('操作成功');
    }
    
    /**
     * 删除通知
     */
    public function delete(Request $request)
    {
        $userId = (int)$this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->notificationModel->delete($id, $userId);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 批量删除通知
     */
    public function batchDelete(Request $request)
    {
        $userId = (int)$this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $ids = $request->param('ids');
        if (empty($ids)) {
            return $this->error('参数错误');
        }
        
        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }
        
        $result = $this->notificationModel->batchDelete($ids, $userId);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 获取未读通知数量
     */
    public function getUnreadCount(Request $request)
    {
        $userId = (int)$this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $count = $this->notificationModel->getUnreadCount($userId);
        
        return $this->success('获取成功', ['count' => $count]);
    }
}
